// PageBakRestore.cpp : implementation file
//

#include "stdafx.h"
#include "finance.h"
#include "CDataFile.h"
#include "financedef.h"
#include "cshellfileop.h"

#include "PageBakRestore.h"

#ifdef _DEBUG
#define new DEBUG_NEW
#undef THIS_FILE
static char THIS_FILE[] = __FILE__;
#endif

/////////////////////////////////////////////////////////////////////////////
// CPageBakRestore property page

IMPLEMENT_DYNCREATE(CPageBakRestore, CPropertyPage)

CPageBakRestore::CPageBakRestore() : CPropertyPage(CPageBakRestore::IDD)
{
	//{{AFX_DATA_INIT(CPageBakRestore)
	m_csBakPath = _T("");
	//}}AFX_DATA_INIT
}

CPageBakRestore::~CPageBakRestore()
{
}

void CPageBakRestore::DoDataExchange(CDataExchange* pDX)
{
	CPropertyPage::DoDataExchange(pDX);
	//{{AFX_DATA_MAP(CPageBakRestore)
	DDX_Text(pDX, IDC_ED_BAKPATH, m_csBakPath);
	DDX_Control(pDX, IDC_BTN_BAKDIR, m_btnSelBak);
	//}}AFX_DATA_MAP
}


BEGIN_MESSAGE_MAP(CPageBakRestore, CPropertyPage)
	//{{AFX_MSG_MAP(CPageBakRestore)
	ON_BN_CLICKED(IDC_BTN_BAKDIR, OnBtnSelBakdir)
	//}}AFX_MSG_MAP
END_MESSAGE_MAP()

/////////////////////////////////////////////////////////////////////////////
// CPageBakRestore message handlers

BOOL CPageBakRestore::OnInitDialog() 
{
	CDataFile	IniDF( MONEY_CFG );
	t_Str		sdStrTmp;
	CHAR		chSQL[MAX_PATH+100] = { 0 };
	DWORD		dwRet;

	
	CPropertyPage::OnInitDialog();
	
	// <> Get current path segment and init edit
	sdStrTmp = IniDF.GetValue( "BakPath", "Backup segment" );
	if( 0 == sdStrTmp.length() )
	{
		dwRet = GetCurrentDirectory( MAX_PATH, chSQL );
		if( dwRet == 0 ) 
		{
			sprintf( chSQL, "GetCurrentDirectory failed (%d)\n", GetLastError() );
		}
		
		if( dwRet > MAX_PATH ) 
		{
			sprintf( chSQL, "GetCurrentDirectory failed (buffer too small; need %d chars)\n", dwRet );
		}

		// otherwise is ok
	}
	else
	{
		sdStrTmp.copy( chSQL, sdStrTmp.length(), 0 );
	}


	m_csBakPath= chSQL;
	
	UpdateData( FALSE );  

	return TRUE;  // return TRUE unless you set the focus to a control
	              // EXCEPTION: OCX Property Pages should return FALSE
}

// 
// folder browse function
//--------------------------------------------------------------------------------
int CALLBACK BrowseCallbackProc( HWND hwnd, UINT uMsg, LPARAM lParam, LPARAM lpData )
{
	switch( uMsg )
	{
	case BFFM_INITIALIZED:
		{
			// add your initialization code here
		}
		break ;

	case BFFM_SELCHANGED:
		{
			TCHAR szText[MAX_PATH] = {0};
			SHGetPathFromIDList(reinterpret_cast<LPITEMIDLIST>(lParam), szText);
			SendMessage(hwnd, BFFM_SETSTATUSTEXT, 0,
						reinterpret_cast<LPARAM>(szText));
		}
		break ;
	}
         
	return 0;
}


// 
// 
//--------------------------------------------------------------------------------
void CPageBakRestore::OnBtnSelBakdir() 
{
	LPMALLOC		pMalloc = NULL;
	LPITEMIDLIST	pidl = NULL;
	BROWSEINFO		bi;
	TCHAR			szPath[ MAX_PATH ];    // not used
	ZeroMemory(&bi, sizeof(BROWSEINFO));
	

	// set the bi's default values
	bi.hwndOwner = m_hWnd;
	bi.lpszTitle = _T("Current folder is:");
	bi.ulFlags	 = BIF_RETURNONLYFSDIRS | BIF_STATUSTEXT;
	bi.lpfn		 = BrowseCallbackProc;
	
	pidl = SHBrowseForFolder(&bi);
	if( pidl != NULL )
	{
		//SHGetPathFromIDList( pidl, m_csBakPath.GetBuffer( m_csBakPath.GetLength() ) ); this line occur ASSERT() !
		SHGetPathFromIDList( pidl, szPath );

		m_csBakPath = szPath;
		UpdateData(FALSE);

		// free memory
		if(SUCCEEDED(SHGetMalloc(&pMalloc)) && pMalloc);
		
		pMalloc->Free(pidl);  
		pMalloc->Release(); 
	}
}

// 
//--------------------------------------------------------------------------------
//DEL void CPageBakRestore::OnBtnSelResdir() 
//DEL {
//DEL 	CHAR			chFile[ MAX_PATH ] = {0};
//DEL 	OPENFILENAME	OpName;
//DEL 
//DEL 
//DEL 
//DEL 	ZeroMemory( &OpName, sizeof(OPENFILENAME) );
//DEL 
//DEL 	OpName.lStructSize	= sizeof(OPENFILENAME);
//DEL 	OpName.hwndOwner	= m_hWnd;
//DEL 	OpName.lpstrFile	= chFile;
//DEL 	OpName.nMaxFile		= sizeof(chFile);
//DEL 	OpName.Flags			= OFN_PATHMUSTEXIST | OFN_FILEMUSTEXIST;
//DEL 	OpName.nFilterIndex		= 1;
//DEL 	OpName.lpstrFileTitle	= NULL;
//DEL 	OpName.nMaxFileTitle	= 0;
//DEL 	OpName.lpstrInitialDir	= NULL;
//DEL 	OpName.lpstrFilter	= _T( "Open backup mdb(*.mdb)\0*.mdb\0" );
//DEL 	
//DEL 	if( !GetOpenFileName(&OpName) )
//DEL 	{
//DEL 		return ;
//DEL 	}
//DEL 
//DEL 	m_csRestoreFile = chFile;
//DEL 
//DEL 	UpdateData(FALSE);
//DEL 	
//DEL }

// 
//--------------------------------------------------------------------------------
void CPageBakRestore::OnOK() 
{

	SaveBakPathSettings();

	CPropertyPage::OnOK();
}



// 
//--------------------------------------------------------------------------------
void CPageBakRestore::SaveBakPathSettings()
{
	CDataFile		IniDF( MONEY_CFG );
	CHAR			chSQL[MAX_PATH + 100] = { 0 };
	

	
	// <> fstream into  ini file
	if( !m_csBakPath.IsEmpty() )
	{
		strcpy( chSQL, m_csBakPath );		
		
		IniDF.SetValue( "BakPath", chSQL, "Path for backup", "Backup segment" );
	}
	else
		IniDF.SetValue( "BakPath", "", "Path for backup", "Backup segment" );
	
	
	IniDF.Save();
}

// 
//--------------------------------------------------------------------------------
//DEL void CPageBakRestore::OnBtnRestore() 
//DEL {
//DEL 	CShellFileOp	op, opcpy;
//DEL 	BOOL			bSuccess = FALSE, bAPICalled = FALSE, bAborted = FALSE;
//DEL 	int				nAPIReturn = 0;
//DEL 	CString			csAbsPath;
//DEL 	CString			strMsg;
//DEL 
//DEL 
//DEL 
//DEL 	// <> Dest abs path for backup file
//DEL 	csAbsPath = m_csRestoreFile;
//DEL 
//DEL 	
//DEL 	// <> Dele Mmoney.mdb first 
//DEL   	if( csAbsPath.IsEmpty() ) 
//DEL   	{
//DEL 		return ;
//DEL 	}
//DEL 
//DEL 	// <> Close Mmoney.mdb first
//DEL 
//DEL 
//DEL 	// <> Del old Mmoney.mdb
//DEL 	op.SetOperationFlags(  FO_DELETE, this,
//DEL 							FOF_FILESONLY | FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR | FOF_RENAMEONCOLLISION );
//DEL 
//DEL 	op.AddSourceFile( "Mmoney.mdb" );
//DEL 
//DEL 	op.AddDestFile( "Mmoney.mdb" );
//DEL 
//DEL 	bSuccess = op.Go( &bAPICalled, &nAPIReturn, &bAborted );
//DEL 
//DEL 	// <> Restore now
//DEL 	if( bSuccess ) 
//DEL 	{
//DEL 		// <> Backup new bak.mdb
//DEL 		opcpy.SetOperationFlags(  FO_COPY, this,
//DEL 								FOF_FILESONLY | FOF_NOCONFIRMATION | FOF_NOCONFIRMMKDIR | FOF_RENAMEONCOLLISION );
//DEL  
//DEL 		opcpy.AddSourceFile( csAbsPath );
//DEL 
//DEL 		opcpy.AddDestFile( "Mmoney.mdb" );
//DEL 
//DEL 		bSuccess = opcpy.Go( &bAPICalled, &nAPIReturn, &bAborted );
//DEL 		if( bSuccess ) 
//DEL 		{
//DEL 			MessageBox( "Mmoney.mdb backup to Mmoney_bak.mdb successfully!", "Remind", MB_ICONINFORMATION );
//DEL 		}
//DEL 		else
//DEL 		{
//DEL 			MessageBox( "Mmoney.mdb backup to Mmoney_bak.mdb unsuccessfully!\nPlease backup the Mmoney.mdb manually!", "Remind", MB_ICONWARNING );
//DEL 		}
//DEL 	}
//DEL 	else
//DEL 	{
//DEL 		strMsg.Format( _T("CShellFileOp::Go() returned %s.\n\nbAPICalled = %s\nnAPIReturn = %d\nbAborted = %s"),
//DEL 						 bSuccess ? _T("TRUE") : _T("FALSE"),
//DEL 						 bAPICalled ? _T("TRUE") : _T("FALSE"),
//DEL 						 nAPIReturn,
//DEL 						 bAborted ? _T("TRUE") : _T("FALSE") );
//DEL  
//DEL 		 AfxMessageBox( strMsg, MB_ICONINFORMATION );
//DEL 	}
//DEL 
//DEL }
